<?php

namespace App\Http\Controllers;

use App\Article;
use App\Movie;
use App\Show;
use App\Vote;
use Illuminate\Http\Request;

class VoteController extends Controller
{


    public function __construct()
    {
        $this->middleware(['auth', 'admin'], ['except' => ['forArticle', 'forShow', 'forMovie', 'forEpisode']]);
    }

    public function forArticle(Request $request) {
        $articleId = $request->input('id');
        $score = $request->score;
        $vote = Vote::FirstOrNew(['article_id'=>$articleId]);
        $vote->score = $score;
        $article = Article::find($articleId);
        $article->vote()->save($vote);
        return response()->json($vote);
    }

    public function forShow(Request $request) {
        $show_id = $request->input('id');
        $score = $request->score;
        $vote = Vote::FirstOrNew(['show_id'=>$show_id]);
        $vote->score = $score;
        $show = Show::find($show_id);
        $show->vote()->save($vote);
        return response()->json($vote);
    }
    public function forMovie(Request $request) {
        $movie_id = $request->input('id');
        $score = $request->score;
        $vote = Vote::FirstOrNew(['movie_id'=>$movie_id]);
        $vote->score = $score;
        $movie = Movie::find($movie_id);
        $movie->vote()->save($vote);
        return response()->json($vote);
    }
}
